﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GITGUI
{
    public partial class Form1 : Form
    {
        String normalOpenFiloder = @"D:\";
        ArrayList CommandList = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            loadTreeView(normalOpenFiloder, TV_floder.Nodes);
        }

        /// <summary>
        /// 加载文件列表
        /// </summary>
        /// <param name="path"></param>
        /// <param name="nodes"></param>
        public void loadTreeView(String path, TreeNodeCollection nodes)
        {
            try
            {
                string[] dirs = Directory.GetDirectories(path);//获取指定目录下的所有文件夹
                foreach (var item in dirs)
                {
                    TreeNode node = nodes.Add(Path.GetFileName(item));
                    loadTreeView(item, node.Nodes);
                }
                String[] files = Directory.GetFiles(path);
                foreach (var file in files)
                {
                    nodes.Add(Path.GetFileName(file));
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 设置当前路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TV_floder_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TB_NowPath.Text = normalOpenFiloder + TV_floder.SelectedNode.FullPath;
        }

        private void TB_Command_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (TB_Command.Text.ToString() == "clear")
                {
                    RTB_OutPut.Text = null;
                }
                RTB_OutPut.Text = RunCommand.RunCommandMethod(TB_Command.Text.ToString());

            }
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Push_Click(object sender, EventArgs e)
        {
            Task task = new Task(Upload);
            Control.CheckForIllegalCrossThreadCalls = false;
            task.Start();
            
        }
        public void Upload()
        {
            if (TB_NowPath.Text.Length > 0)
            {
                String root = normalOpenFiloder.Substring(0, 2);
                String cdCommand = "cd " + TB_NowPath.Text.ToString();
                String gitAdd = "git add " + TB_gitAdd.Text.ToString();
                String gitcommit = $"git commit -{CB_CommitCommand.Text.ToString()}\"{TB_Commit.Text.ToString()}\"";
                String pushCommand = "git push origin master";
                CommandList.Add(root);
                CommandList.Add(cdCommand);
                CommandList.Add(gitAdd);
                CommandList.Add(gitcommit);
                CommandList.Add(pushCommand);
                RunCommand.RunCommandMethod(CommandList, RTB_OutPut);
                CommandList.Clear();
            }
            else
            {
                MessageBox.Show("目标路径不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void btn_clone_Click(object sender, EventArgs e)
        {
            Task task = new Task(clone);
            task.Start();
        }
        public void clone()
        {
            String target = TB_Floder.Text.ToString();
            if (target.Length > 0)
            {
                String gitCloneCommand = "git clone " + TB_GitUrl.Text.ToString();
                String targetFloder = TB_Floder.Text.ToString();
                String targetRoot = target.Substring(0, 2);
                CommandList.Add(targetRoot);
                CommandList.Add(gitCloneCommand);
                RunCommand.RunCommandMethod(CommandList, RTB_OutPut);
                CommandList.Clear();
            }
            else
            {
                MessageBox.Show("目标路径不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void TB_Floder_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SelectFloder.ShowDialog();
            String target = SelectFloder.SelectedPath;
            TB_Floder.Text = target;
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            Task task;
            switch (e.ClickedItem.Tag.ToString())
            {
                case "1":
                    TV_floder.Nodes.Clear();
                    SelectFloder.ShowDialog();
                    String newPath = SelectFloder.SelectedPath;
                    loadTreeView(newPath, TV_floder.Nodes);
                    break;
                case "2":
                    String root = normalOpenFiloder.Substring(0, 2);
                    String cdCommand = "cd " + TB_NowPath.Text.ToString();
                    String repair = "git push -f origin master";
                    CommandList.Add(root);
                    CommandList.Add(cdCommand);
                    CommandList.Add(repair);
                    RunCommand.RunCommandMethod(CommandList, RTB_OutPut);
                    CommandList.Clear();
                    break;
                case "3":
                    break;
            }
        }

        /// <summary>
        /// 新建仓库选取地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TB_LocalWarehouse_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SelectFloder.ShowDialog();
            String target = SelectFloder.SelectedPath;
            TB_LocalWarehouse.Text = target;
            normalOpenFiloder = target.Substring(0, 3);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String path = TB_LocalWarehouse.Text.ToString().Substring(0,2);
            String cdPath = "cd "+TB_LocalWarehouse.Text.ToString();
            CommandList.Add(path);
            CommandList.Add(cdPath);
            CommandList.Add("git clone "+ TB_GitWarehouse.Text.ToString());
            RunCommand.RunCommandMethod(CommandList,RTB_OutPut);
        }
    }
}
